/** 
 * projectName:  
 * fileName:  
 * packageName:  
 * date: 
 * copyright(c) 2017-2020 xxx公司
 */
package cn.message.web;

import cn.itcast.pojo.evaluate;
import cn.itcast.pojo.user;
import cn.itcast.util.CookieUtil;
import cn.itcast.util.OutputStringController;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: ${用户评论控制类}
 * @return ${return_type}
 * @throws
 * @author 徐威
 * @date 2019/6/17 9:34
 */

@Slf4j
@RestController
@RequestMapping(value="/wxshop/message",produces="text/html;charset=UTF-8")
public class MessageEvaluateController {

	@Resource
	private cn.message.service.evaluateService evaluateService;
	@Resource
    OutputStringController osc;
	@Resource
    CookieUtil cookieUtil;

	/**
	 * @Description: ${1,获取所有商品评论}
	 * @return ${return_type}
	 * @throws
	 * @author 徐威
	 * @date 2019/6/17 9:28
	 */
	@PostMapping("/findAllEvaluate")
	public String findAllEvaluate(@RequestBody Map<String,String> map)throws Exception{
		//返回结果
		String results ="";
        //获取页数
        Integer pageNum = Integer.parseInt(map.get("page"));
        //获取每页大小
        Integer pageSize = Integer.parseInt(map.get("limit"));
        //查询类型
        String select = "";
        if(StringUtils.isNotBlank(map.get("select"))){
            select = map.get("select");
        }
        //查询值
        String value = "";
        if(StringUtils.isNotBlank(map.get("value"))){
            value = map.get("value");
        }
        List<evaluate> evaluates = evaluateService.findAllProduct(pageNum,pageSize,select,value);
        if(evaluates.isEmpty()){
            results = osc.resultFailure("no","");
            log.debug("获取所有产品信息失败");
        }else {
            // 取分页信息
            PageInfo<evaluate> pageInfo = new PageInfo<evaluate>(evaluates);
            long total = pageInfo.getTotal(); //获取总记录数
            results = osc.resultSuccess("yes",evaluates,total);
            log.debug("获取所有产品信息成功");
        }
		return results;

	}
	/**
	 *功能：查找所有用户
	 * @author 欧阳辉
	 * @date 2019/6/19
	  * @param map
	 * @return java.lang.String
	 */
    @PostMapping("/findAllUser")
    public String findAllUser(@RequestBody Map<String,String> map)throws Exception{
        //返回结果
        String results ="";
        //获取页数
        Integer pageNum = Integer.parseInt(map.get("page"));
        //获取每页大小
        Integer pageSize = Integer.parseInt(map.get("limit"));
        //查询类型
        String select = "";
        if(StringUtils.isNotBlank(map.get("select"))){
            select = map.get("select");
        }
        //查询值
        String value = "";
        if(StringUtils.isNotBlank(map.get("value"))){
            value = map.get("value");
        }
        List<user> users = evaluateService.findAllUser(pageNum,pageSize,select,value);
        if(users.isEmpty()){
            results = osc.resultFailure("no","");
            log.debug("获取所有产品信息失败");
        }else {
            // 取分页信息
            PageInfo<user> pageInfo = new PageInfo<user>(users);
            long total = pageInfo.getTotal(); //获取总记录数
            results = osc.resultSuccess("yes",users,total);
            log.debug("获取所有产品信息成功");
        }
        return results;

    }

	/**
	 * @Description: ${2,查找评价}
	 * @return ${return_type}
	 * @throws
	 * @author 徐威
	 * @date 2019/6/17 10:22
	 */
	@PostMapping("/findEvaluate")
	public String findEvaluate(@RequestBody Map<String,String> map)throws Exception{
		Integer productId = Integer.valueOf(map.get("productId"));
		//返回结果
		String results ="";
		List<evaluate> evaluate = evaluateService.getEvaluate(productId);
		if (evaluate.isEmpty()) {
			results = osc.resultFailure("no", "");
			log.debug("查找评价失败");
		} else {
			results = osc.resultSuccess("yes", evaluate);
			log.debug("查找评价成功");
		}
		return results;
	}

	/**
	 * @Description: ${3,删除评价}
	 * @return ${return_type}
	 * @throws
	 * @author 徐威
	 * @date 2019/6/17 14:57
	 */
	@PostMapping("/delectEvaluate")
	public String delectEvaluate(@RequestBody Map<String,String> map){
		log.debug("删除评价");
		//获取地址信息
		Integer evaluateId =Integer.parseInt(map.get("evaluateId"));
		//返回结果
		String results = "";
		boolean isdelect = evaluateService.delectEvaluate(evaluateId);
		if(isdelect){
			results = osc.success("yes");
			log.debug("删除评价成功");
		}else{
			results = osc.failure("no");
			log.debug("删除评价失败");
		}

		return results;
	}


	
}






